﻿/*
 *Author: Jeff Liu 
 *
 *Under MIT License
 *jebberwocky@gmail.com
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using System.IO;

namespace Citiport.Util
{
    /// <summary>
    /// Class for converting JSON string to objects
    /// The JSON reader class is from 
    /// http://james.newtonking.com/archive/2006/06/26/571.aspx
    /// Author: Jeff
    /// Date: 04/09/2008
    /// </summary>
    public class JSON
    {
        public class TranslatedData
        {
            public string translatedText {get;set;}
            public string detectedSourceLanguage {get;set;}
        }

        public class DetectData
        {
            public string language { get; set; }
            public Boolean isReliable { get; set; }
            public Double confidence { get; set; }
        }

        public class TranslatedText
        {
            public TranslatedData responseData { get; set; }
            public string responseDetails { get; set; }
            public long responseStatus { get; set; }
        }

        public class DetectedLang
        {
            public DetectData responseData { get; set; }
            public string responseDetails { get; set; }
            public string responseStatus { get; set; }
        }

        /// <summary>
        /// Convert JSON string to data object for Google Translate
        /// </summary>
        /// <param name="json">Formatted JSON string</param>
        /// <returns>Data Object</returns>
        public static TranslatedText parseTranslatedText(String json)
        {
            TranslatedText tt = new TranslatedText();
            TranslatedData td = new TranslatedData();
            
            JsonReader reader = new JsonReader(new StringReader(json));
            while (reader.Read())
            {
                if (reader.TokenType == JsonToken.PropertyName)
                {
                    switch (reader.Value.ToString().Trim().ToLower())
                    {
                        case "translatedtext":
                            {
                                reader.Read();
                                td.translatedText = reader.Value.ToString().Trim();
                                break;
                            }
                        case "detectedsourcelanguage":
                            {
                                reader.Read();
                                if (reader.Value == null)
                                    td.detectedSourceLanguage = null;
                                else
                                    td.detectedSourceLanguage = reader.Value.ToString().Trim();
                                break;
                            }
                        case "responsedetails":
                            {
                                reader.Read();
                                if (reader.Value == null)
                                    tt.responseDetails = null;
                                else
                                    tt.responseDetails = reader.Value.ToString().Trim();
                                break;
                            }
                        case "responsestatus":
                            {
                                reader.Read();
                                tt.responseStatus = Convert.ToInt64(reader.Value);
                                break;
                            }
                    }
                }
            }
            tt.responseData = td;
            return tt;
        }

        /// <summary>
        /// Convert JSON string to data object for Google Language detect
        /// </summary>
        /// <param name="json">Formatted JSON string</param>
        /// <returns>Data Object</returns>
        public static DetectedLang parseDetectedLang(String json)
        {
            DetectData dd = new DetectData();
            DetectedLang dl = new DetectedLang();
            JsonReader reader = new JsonReader(new StringReader(json));
            while (reader.Read())
            {
                if (reader.TokenType == JsonToken.PropertyName)
                {
                    switch (reader.Value.ToString().Trim().ToLower())
                    {
                        case "language":
                            {
                                reader.Read();
                                dd.language = reader.Value.ToString().Trim();
                                break;
                            }
                        case "isreliable":
                            {
                                reader.Read();
                                dd.isReliable = Convert.ToBoolean(reader.Value);
                                break;
                            }
                        case "confidence":
                            {
                                reader.Read();
                                dd.confidence = Convert.ToDouble(reader.Value.ToString());
                                break;
                            }
                        case "responsedetails":
                            {
                                reader.Read();
                                if (reader.Value == null)
                                    dl.responseDetails = null;
                                else
                                    dl.responseDetails = reader.Value.ToString().Trim();
                                break;
                            }
                        case "responsestatus":
                            {
                                reader.Read();
                                dl.responseStatus = reader.Value.ToString().Trim();
                                break;
                            }
                    }
                }
            }
            dl.responseData = dd;
            return dl;
        }
    }
}
